package com.xinyi.share.baidu;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.xinyi.share.utils.RequestUtils;
import com.xinyi.share.utils.SecurityKeys;

/**
 * 
 * @ClassName: BaiduCore
 * @Description: 百度工具
 * @author wangchuan
 * @date 2014年10月17日 上午11:25:00
 *
 */
public final class BaiduCore {
	private final static Logger logger = Logger.getLogger(BaiduCore.class);
	/**
	 * IP定位API请求url
	 */
	private final static String ipurl = "http://api.map.baidu.com/location/ip";
	/**
	 * 天气API请求URL
	 */
	private final static String weatherurl = "http://api.map.baidu.com/telematics/v3/weather";
	/**
	 * 将地址转换成坐标url
	 */
	private final static String geocodingurl = "http://api.map.baidu.com/geocoder/v2/";

	/**
	 * 
	 * @Title: ip
	 * @Description: 获取当前请求地址
	 * @return
	 * @return String
	 * @throws
	 */
	public static JSONObject getIp() {
		return getIp(null);
	}

	/**
	 * 
	 * @Title: ip
	 * @Description: 更具IP获取地址
	 * @param ip
	 * @return
	 * @return String
	 * @throws
	 */
	public static JSONObject getIp(String ip) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("ak", SecurityKeys.getInstance().getBaiduConfig().getAk());
		if (StringUtils.isNotEmpty(ip))
			params.put("ip", ip);
		params.put("coor", "bd09ll");
		String ipres = RequestUtils.get(ipurl, params);
		JSONObject jsonObject = formatToJsonObject(ipres);
		if (checkResultStatu(jsonObject)) {
			return jsonObject;
		} else {
			logger.error("IP获取地址返回结果验证状态失败!");
			return null;
		}
	}

	/**
	 * @throws UnsupportedEncodingException
	 * 
	 * @Title: getGeocoding
	 * @Description: 根据地址反查坐标
	 * @return
	 * @return String
	 * @throws
	 */
	public static JSONObject getGeocoding(String address) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("address", address);
		params.put("output", "json");
		params.put("ak", SecurityKeys.getInstance().getBaiduConfig().getAk());
		String geocodingres = RequestUtils.get(geocodingurl, params);
		JSONObject jsonObject = formatToJsonObject(geocodingres);
		if (checkResultStatu(jsonObject)) {
			return jsonObject;
		} else {
			logger.error("根据地址反查坐标返回结果验证状态失败!");
			return null;
		}
	}

	/**
	 * 
	 * @Title: getWeather
	 * @Description: 获取天气
	 * @param location
	 * @return
	 * @return String
	 * @throws
	 */
	public static JSONObject getWeather(String location) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("ak", SecurityKeys.getInstance().getBaiduConfig().getAk());
		params.put("location", location);
		params.put("output", "json");
		String weatherres = RequestUtils.get(weatherurl, params);
		JSONObject jsonObject = formatToJsonObject(weatherres);
		if (checkResultStatu(jsonObject)) {
			return jsonObject;
		} else {
			logger.error("获取天气返回结果验证状态失败!");
			return null;
		}
	}
	/**
	 * 
	 * @Title: getWeatherByIP
	 * @Description: 根据请求IP获取天气
	 * @return
	 * @return JSONObject
	 * @throws
	 */
	public static JSONObject getWeatherByIP() {
		return getWeatherByIP(null);
	}
	/**
	 * 
	 * @Title: getWeatherByIP
	 * @Description: 根据IP获取天气
	 * @param ip
	 * @return
	 * @return JSONObject
	 * @throws
	 */
	public static JSONObject getWeatherByIP(String ip) {
		JSONObject ipJsonObject = getIp(ip);
		if (null != ipJsonObject&&ipJsonObject.containsKey("content")) {
			JSONObject contentJsonObject = ipJsonObject.getJSONObject("content");
			if(contentJsonObject.containsKey("point")){
				JSONObject pointJsonObject = contentJsonObject.getJSONObject("point");
				StringBuilder locationParam = new StringBuilder();
				locationParam.append(pointJsonObject.getString("x")).append(",").append(pointJsonObject.getString("y"));
				JSONObject weatherJsonObject = getWeather(locationParam.toString());
				return weatherJsonObject;
			}
		}
		return null;
	}

	/**
	 * 
	 * @Title: formatToJsonObject
	 * @Description: 将res转换为jsonobject
	 * @param res
	 * @return
	 * @return JSONObject
	 * @throws
	 */
	private static JSONObject formatToJsonObject(String res) {
		JSONObject jsonObject = JSONObject.fromObject(res);
		return jsonObject;
	}

	/**
	 * 
	 * @Title: checkResultStatu
	 * @Description: 验证返回结果是否正确
	 * @param object
	 * @return boolean
	 * @throws
	 */
	private static boolean checkResultStatu(JSONObject object) {
		if (object.containsKey("status")) {
			String statu = object.getString("status");
			if (StringUtils.equals(statu, "success") || StringUtils.equals(statu, "0")) {
				return true;
			}
		}
		return false;
	}
}
